/*
 * Copyright 2011-2011 Tiago da Fonseca Frazao  All rights reserved.
 */

package mz.co.research.shared.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * @author Tiago da Fonseca Frazao
 */
@Entity
@Table(name = "RESEARCH")
public class Research extends UpdatableEntity {

	@NotNull
	@Column(name = "CODE", length = 25, nullable = false)
	private String code;

	@NotNull
	@Size(min = 10, max = 255)
	@Column(name = "DESCRIPTION", length = 255, nullable = false)
	private String description;

	@OneToMany(mappedBy = "research", cascade = CascadeType.PERSIST)
	private List<Form> forms = new ArrayList<Form>();

	@Transient
	private Map<String, Form> formMap = new HashMap<String, Form>();

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @param the
	 *            form to add
	 */
	public void addForm(Form form) {
		form.setResearch(this);
		this.forms.add(form);
		this.formMap.put(form.getCode(), form);
	}

	/**
	 * @param code
	 * @return
	 */
	public Form getForm(String code) {
		return formMap.get(code);
	}

	/**
	 * @param form
	 *            the form to remove
	 */
	public void removeForm(Form form) {
		this.forms.remove(form);
		this.formMap.remove(form.getCode());
	}

	@Override
	public String toString() {
		StringBuilder buffer = new StringBuilder("\nResearch");

		buffer.append(super.toString());

		buffer.append("\ndescription :" + this.description);

		return buffer.toString();
	}
}
